<template>
	<ContentWrap v-if="isMemberDetail">
		<!-- 搜索工作栏 -->
		<el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
			<el-form-item label="红娘昵称" prop="name">
				<el-input v-model="queryParams.name" placeholder="请输入红娘昵称" clearable class="!w-240px" />
			</el-form-item>
			<el-form-item>
				<el-button @click="handleQuery">
					<Icon icon="ep:search" class="mr-5px" /> 搜索
				</el-button>
				<el-button @click="resetQuery">
					<Icon icon="ep:refresh" class="mr-5px" /> 重置
				</el-button>
				<el-button type="primary" plain @click="openForm('create')"
					v-hasPermi="['blind:matchmakerList:create']">
					<Icon icon="ep:plus" class="mr-5px" /> 新增
				</el-button>
			</el-form-item>
		</el-form>
	</ContentWrap>

	<!-- 列表 -->
	<ContentWrap>
		<el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
			<el-table-column label="红娘编码" align="center" prop="no" width="200" />
			<el-table-column label="红娘昵称" align="center" prop="name" />
			<el-table-column label="红娘手机号" align="center" prop="phone" />

			<el-table-column label="累计会员" align="center" prop="memberNum" />
			<el-table-column label="本月会员" align="center" prop="monthMemberNum" />
			<el-table-column label="牵线总数(次)" align="center" prop="stringingTotalNum" />
			<el-table-column label="本月牵线(次)" align="center" prop="stringingMonthNum" />
			<el-table-column label="操作" align="center" fixed="right" width="200">
				<template #default="scope">
					<div class="flex items-center justify-center">
						<el-button link type="primary" @click="openDetail(scope.row.id)">详情</el-button>
						<el-button link type="primary" @click="openForm('update', scope.row.id)"
							v-hasPermi="['blind:matchmakerList:update']">
							编辑
						</el-button>
						<el-button link type="danger" @click="handleDelete(scope.row.id)"
							v-hasPermi="['blind:matchmakerList:delete']">
							删除
						</el-button>
						<el-dropdown @command="(command) => handleCommand(command, scope.row)"
							v-hasPermi="['blind:matchmakerList:resetPwd']">
							<el-button type="primary" link>
								<Icon icon="ep:d-arrow-right" /> 更多
							</el-button>
							<template #dropdown>
								<el-dropdown-menu>
									<el-dropdown-item command="handleResetPwd"
										v-if="checkPermi(['system:user:update-password'])">
										<Icon icon="ep:key" />重置密码
									</el-dropdown-item>
								</el-dropdown-menu>
							</template>
						</el-dropdown>
					</div>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页 -->
		<Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
			@pagination="getList" />
	</ContentWrap>
	<!-- 表单弹窗：添加/修改 -->
	<Form ref="formRef" @success="getList" />
	<!-- 详情弹窗：查看 -->
	<Detail ref="formRefDetail" />
</template>

<script setup lang="ts">
	import * as MatchmakerInfoApi from '@/api/blind/maker/MatchmakerInfo'
	import Form from './Form.vue'
	import Detail from './Detail.vue'
	import * as UserApi from '@/api/system/user'
	import { checkPermi } from '@/utils/permission'

	defineOptions({ name: 'BlindMemberUser' })

	const message = useMessage() // 消息弹窗
	const { t } = useI18n() // 国际化

	const { user } = withDefaults(defineProps<{ user ?: any }>(), { user: null })
	// 获取路由
	const router = useRouter()
	const routerTitle = router.currentRoute.value.meta.title

	// 是否为会员详情页面
	const isMemberDetail = computed(() => routerTitle !== '会员详情')

	const loading = ref(true) // 列表的加载中
	const total = ref(0) // 列表的总页数
	const list = ref([]) // 列表的数据
	const queryParams = reactive({
		pageNo: 1,
		pageSize: 10,
		name: undefined
	})
	const queryFormRef = ref() // 搜索的表单

	/** 查询列表 */
	const getList = async () => {
		loading.value = true
		try {
			const data = await MatchmakerInfoApi.getMatchmakerInfoPage(queryParams)

			list.value = data.list
			total.value = data.total
		} finally {
			loading.value = false
		}
	}

	/** 搜索按钮操作 */
	const handleQuery = () => {
		queryParams.pageNo = 1
		getList()
	}

	/** 重置按钮操作 */
	const resetQuery = () => {
		queryFormRef.value.resetFields()
		handleQuery()
	}

	/** 添加/修改操作 */
	const formRef = ref()
	const openForm = (type : string, id ?: number) => {
		formRef.value.open(type, id)
	}

	/** 打开会员详情 */
	const formRefDetail = ref()
	const openDetail = (id ?: number) => {
		formRefDetail.value.open(id)
	}
	/** 操作分发 */
	const handleCommand = (command : string, row : MatchmakerInfoApi.MatchmakerVO) => {
		switch (command) {
			case 'handleResetPwd':
				handleResetPwd(row.userId)
				break
			default:
				break
		}
	}


	/** 重置密码 */
	const handleResetPwd = async (userId ?: number) => {
		try {
			// 重置的二次确认
			const result = await message.prompt(
				'请输入新密码',
				t('common.reminder')
			)
			const password = result.value
			// 发起重置
			await UserApi.resetUserPwd(userId, password)
			message.success('修改成功，新密码是：' + password)
		} catch { }
	}

	/** 删除按钮操作 */
	const handleDelete = async (id : number) => {
		try {
			// 删除的二次确认
			await message.delConfirm()
			// 发起删除
			await MatchmakerInfoApi.deleteMatchmakerInfo(id)
			message.success(t('common.delSuccess'))
			// 刷新列表
			await getList()
		} catch { }
	}

	/** 初始化 **/
	onMounted(() => {
		getList()
	})
</script>